package com.shigen.file.serializer;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Date;

/**
 * 日期时间格式化
 *
 * @author shigenfu
 * @date 2022/5/11 3:58 下午
 */
public class DateTimeSerializer extends JsonSerializer<Date> {

  @Override
  public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    if (date != null) {
      String s = DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN);
      jsonGenerator.writeString(s);
    } else {
      jsonGenerator.writeString("");
    }
  }
}
